import { defineStore, createPinia } from 'pinia'
// 引入持久化插件
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
// 引入用户仓库
import { useUserStore } from './modules/user'

export const Store = defineStore({
  // id: 必须的，在所有 Store 中唯一
  id: 'globalState',
  // state: 返回对象的函数
  state: () => ({}),
  getters: {
    // 获取用户信息
    userInfo: () => {
      const userStore = useUserStore()
      return userStore.userInfo
    },
    // 获取用户姓名
    userName: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.name || ''
    },
    // 获取用户头像
    userAvatar: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.avatar || ''
    },
    // 获取用户电话
    userPhone: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.phone || ''
    },
    // 获取用户邮箱
    userEmail: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.email || ''
    },
    // 获取用户员工编号
    employeeNumber: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.employeeNumber || ''
    },
    // 获取用户部门ID
    userDepartmentId: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.departmentId || ''
    },
    // 获取用户权限级别
    userPermissionLevel: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.permissionLevel || 0
    },
    // 获取用户角色权限
    userRoles: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.roles || []
    },
    // 获取用户操作权限
    userPermissions: () => {
      const userStore = useUserStore()
      return userStore.permissions || []
    },
    // 获取用户ID
    userId: () => {
      const userStore = useUserStore()
      return userStore.userInfo?.id || ''
    },
  },
  actions: {},
  persist: {
    // 本地存储的名称
    key: 'globalState',
    //保存的位置
    storage: window.sessionStorage, //localstorage
  },
})

const pinia = createPinia()
//pinia使用
pinia.use(piniaPluginPersistedstate)
export default pinia
